package com.xm.spring5.service;

import com.xm.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(timeout = 5,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)  //事务注解（添加到方法上或者类上）
public class UserService {

    @Autowired
    private UserDao userDao;

    public void accountMoney() {
//        try {
            //第一步：开启事务

            //第二步：业务操作
            userDao.reduceMoney();
            int i = 1 / 0;
            userDao.addMoney();

            //第三步：没有异常，提交事务
//        } catch (Exception e) {

            //第四步：出现异常，事务回滚
//        }
    }
}
